package com.simpleframe.persistence.jdbc;

import java.sql.*; 

import com.simpleframe.env.Configuration;

public class JdbcUtil {
	private final static String DB_DRIVER = Configuration.getInstance().getValue("jdbc.driver"); 
    private final static String DB_CONNECTION = Configuration.getInstance().getValue("jdbc.url"); 
    private final static String DB_NAME = Configuration.getInstance().getValue("jdbc.username"); 
    private final static String DB_PWd = Configuration.getInstance().getValue("jdbc.password");  
      
    static {
        try {  
            Class.forName(DB_DRIVER);  
        } catch (ClassNotFoundException e) {  
            e.printStackTrace();  
        }  
    }  
  
    public static Connection getConnection() {  
        Connection conn = null;  
        try {  
            conn = DriverManager  
                    .getConnection(DB_CONNECTION,  
                            DB_NAME, DB_PWd);  
        } catch (SQLException e) {  
            e.printStackTrace();  
        }  
        return conn;  
    }  
  
    public static Statement getStatement(Connection conn) {  
        Statement stmt = null;  
        try {  
            stmt = conn.createStatement();  
        } catch (SQLException e) {  
            e.printStackTrace();  
        }  
        return stmt;  
    }  
  
    public static PreparedStatement getPreparedStatement(Connection conn, String sql) {  
        PreparedStatement pstmt = null;  
        try {  
            pstmt = conn.prepareStatement(sql);  
        } catch (SQLException e) {  
            e.printStackTrace();  
        }  
        return pstmt;  
    }  
  
    public static ResultSet getRs(Statement stmt, String sql) {  
        ResultSet rs = null;  
        try {  
            rs = stmt.executeQuery(sql);  
        } catch (SQLException e) {  
            e.printStackTrace();  
        }  
        return rs;  
    }  
  
  
    public static void close(Statement stmt) {  
        if (stmt != null) {  
            try {  
                stmt.close();  
            } catch (SQLException e) {  
                e.printStackTrace();  
            }  
        }  
    }  
  
    public static void close(ResultSet rs) {  
        if (rs != null) {  
            try {  
                rs.close();  
            } catch (SQLException e) {  
                e.printStackTrace();  
            }  
        }  
    }  
  
    public static void close(Connection conn) {  
        if (conn != null) {  
            try {  
                conn.close();  
            } catch (SQLException e) {  
                e.printStackTrace();  
            }  
        }  
    }  
}
